﻿#region 命名空间

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// LogManage 的摘要说明。
    /// </summary>
    public class LogManage
    {
        #region - 变量 -

        private static readonly LogDal Dal = new LogDal();

        #endregion

        #region - 方法 -

        #region 添加日志

        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="model"></param>
        public static void Add(Log model)
        {
            if (string.IsNullOrEmpty(model.UserName))
            {
                if (UserManage.Current == null)
                {
                    return;
                }
                model.UserName = UserManage.Current.UserName;
            }
            if (HttpContext.Current != null)
            {
                model.PageName = HttpContext.Current.Request.RawUrl;
            }
            else
            {
                model.PageName = "";
            }
            model.CheckInDate = DateTime.Now;

            Dal.Add(model);
        }

        #endregion

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="ids"></param>
        public Result Delete(string ids)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ids))
            {
                res.Errors = "至少选择一个!";
            }
            else
            {
                Dal.Delete(MyConvert.ConvertToArr(ids));

                Log logModel = new Log
                {
                    Type = "日志管理",
                    Event = "删除日志,ID为:" + ids
                };
                Add(logModel);
            }
            return res;
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        public void Clear()
        {
            Dal.Clear();

            Log logModel = new Log
            {
                Type = "日志管理",
                Event = "管理员 " + UserManage.Current.UserName + " 清空日志"
            };
            Add(logModel);
        }

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="deleted"></param>
        /// <param name="type"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Log> BindList(bool deleted, string type, PagingParam pp)
        {
            return Dal.BindList(deleted, type, pp);
        }

        /// <summary>
        /// 显示日志类型Tab
        /// </summary>
        /// <returns></returns>
        public IList<Log> GroupBy()
        {
            return Dal.GroupBy();
        }

        #endregion
    }
}